//package com.shuqi.activity.Fragments;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import android.app.Activity;
//import android.content.Intent;
//import android.os.Bundle;
//import android.os.Message;
//import android.support.v4.view.ViewPager;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.view.ViewGroup;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
//import android.widget.AdapterView.OnItemSelectedListener;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//import android.widget.TextView;
//
//import com.shuqi.activity.ActivityManager;
//import com.shuqi.activity.BookSearchParentActivity;
//import com.shuqi.activity.BookSearchResultActivity;
//import com.shuqi.activity.adapter.BookSearchImageAdapter;
//import com.shuqi.activity.adapter.BookSearchLabellistAdapter;
//import com.shuqi.activity.adapter.BookSearchLabellistAdapter.OnGridItemClickListener;
//import com.shuqi.activity.adapter.BookSearchLabellistRealAdapter;
//import com.shuqi.activity.viewport.BookSearchFooterView;
//import com.shuqi.activity.viewport.BookSearchGallery;
//import com.shuqi.activity.viewport.BookSearchGalleryHead;
//import com.shuqi.common.ControlHandler;
//import com.shuqi.common.LocalBookInfoManager;
//import com.shuqi.common.utils.Util;
//import com.shuqi.controller.BookSearchRecoomendController;
//import com.shuqi.controller.MainController;
//import com.shuqi.controller.R;
//import com.shuqi.database.dao.impl.SearchHistoryDao;
//import com.shuqi.database.model.BookInfo;
//import com.shuqi.interfaces.EventTypeConstants;
//import com.shuqi.interfaces.IEventListener;
//import com.shuqi.model.bean.SearchRecommendInfo;
//import com.shuqi.model.net.listener.HttpRequestErrListener;
//
///**
// * 搜索推荐
// * 
// * @author linyuren
// * 
// */
//public class BookSearchRecommendFragment extends FragmentBase implements IEventListener,
//        OnClickListener, HttpRequestErrListener, OnGridItemClickListener {
//
//    private BookSearchParentActivity mContext;
//
//    /** 当前界面布局 */
//    private View mCurretntLayout;
//
//    /** 进度圈 */
//    private LinearLayout mLoadingLayout;
//
//    /** 网络错误 */
//    private View mActNoNetwork;
//
//    /** 无数据 */
//    private LinearLayout mActNoDataView;
//
//    /** 热搜推荐列表 */
//    private ListView mSearchLabellist;
//
//    /** 自定义view 作为header添加到listview中 */
//    private BookSearchGalleryHead mBookSearchGalleryHeadView;
//    /** 自定义view作为 footer添加到listview中 */
//    private BookSearchFooterView mChangeLabelFooterView;
//    /** 自定义gallery */
//    private BookSearchGallery mBookSearchGallery;
//    /** ViewPager */
//    private ViewPager mViewPager;
//
//    /** 换下一批标签按钮 */
//    private TextView mSearchTableBatchButton;
//
//    /** 搜索Controller */
//    private BookSearchRecoomendController mBookSearchRecoomendController;
//
//    private final static int INIT = EventTypeConstants.BASE;
//    private final static int INITDATAHOTSUCCESS = INIT + 1;
//    private final static int INITDATAHOTTABLESUCCESS = INIT + 2;
//    private final static int READCACHE = INIT + 3;
//    private final static int SHOW_404 = INIT + 4;
//    private final static int INITDATAHOTSUCCESS_NODATA = SHOW_404 + 1;
//
//    /** 记录第几次去请求热搜推荐数据 */
//    private int mRequsetHotTime = 0;
//
//    /** 是否是请求换一批热搜词 */
//    private boolean mIsRequsetHot;
//
//    /** 搜索主页列表适配器 */
//    private BookSearchLabellistAdapter mBookSearchLabellistAdapter;
//    /** 搜索主页列表适配器 */
//    private BookSearchLabellistRealAdapter mBookSearchLabellistRealAdapter;
//    /** 搜索主页gallery适配器 */
//    private BookSearchImageAdapter mBookSearchImageAdapter;
//
//    /** 存放标签数据 */
//    private List<BookInfo> mSearchLabelInfoData = new ArrayList<BookInfo>();
//    /** 存放gallery数据 */
//    private List<BookInfo> mSearchGalleryInfoData = new ArrayList<BookInfo>();
//
//
//    ControlHandler handler = new ControlHandler(this) {
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case INIT:
//                    fragmentLogicForward();
//                    break;
//                case INITDATAHOTSUCCESS:
//                    fragmentInitData();
//                    break;
//                case INITDATAHOTSUCCESS_NODATA:
//                    mShowNoData();
//                    break;
//                case INITDATAHOTTABLESUCCESS:
//                    mBookSearchLabellistRealAdapter.setRealAdapter(mSearchLabelInfoData);
//                    mBookSearchLabellistRealAdapter.notifyDataSetChanged();
//                    break;
//                case READCACHE:
//                    mLoadingLayout.setVisibility(View.GONE);
//                    readLocalHotSearchImgData();
//                    readLocalHotSearchTableData();
//                    break;
//                case SHOW_404:
//                    if (mRequsetHotTime > 0) {
//                        mIsRequsetHot = true;
//                        mContext.showMsg("无网络，请连接网络后重试");
//                        return;
//                    } else {
//                        mActNoNetwork.setVisibility(View.VISIBLE);
//                        // mContext.showMsg("网络异常..");
//                    }
//                    break;
//                default:
//                    break;
//            }
//        };
//    };
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//
//        this.mBookSearchRecoomendController =
//                (BookSearchRecoomendController) MainController.getMainManagerInstance()
//                        .createController(MainController.CONTROLLER_ID_SEARCHALL, mContext);
//        this.mBookSearchRecoomendController.addEventListener(this);
//
//        if (container == null) {
//            return null;
//        }
//        if (inflater == null) {
//            inflater = LayoutInflater.from(mContext);
//        }
//        // 第一次实例化,bundle为传入参数
//        if (savedInstanceState == null) {
//            savedInstanceState = getArguments();
//        }
//        mCurretntLayout =
//                inflater.inflate(R.layout.book_seatch_recommended_content, container, false);
//
//        handler.sendEmptyMessage(INIT);
//        return mCurretntLayout;
//    }
//
//    /** 构造函数 */
//    public BookSearchRecommendFragment(ViewPager viewPager) {
//        this.mViewPager = viewPager;
//    }
//
//    @Override
//    public void onAttach(Activity activity) {
//        if (mContext == null) {
//            mContext = (BookSearchParentActivity) activity;
//        }
//        super.onAttach(activity);
//    }
//
//
//    @Override
//    public void onResume() {
//        super.onResume();
//    }
//
//    @Override
//    public void onClick(View v) {
//        int id = v.getId();
//        switch (id) {
//            case R.id.search_table_batchbt: // 换一批热搜词按钮
//                if (this.mIsRequsetHot) {
//                    mBookSearchRecoomendController.handle(0, "hot", "");
//                    this.mIsRequsetHot = false;
//                }
//                break;
//            case R.id.retry:
//                if (!Util.isNetworkConnected(mContext)) {
//                    return;
//                } else {
//                    mActNoDataView.setVisibility(View.GONE);
//                    mLoadingLayout.setVisibility(View.VISIBLE);
//                    mBookSearchRecoomendController.handle(0, "hot", "");
//                }
//                break;
//        }
//    }
//
//    @Override
//    public void onEvent(int event, Object data) {
//        switch (event) {
//            case EventTypeConstants.EVENT_SUCCESS:// 读取数据 写处理逻辑
//                SearchRecommendInfo srInfo = (SearchRecommendInfo) data;
//                if (srInfo != null
//                        && ((srInfo.getBooksList() != null && srInfo.getBooksList().size() > 0) || (srInfo
//                                .getLabelList() != null && srInfo.getLabelList().size() > 0))) {
//                    formatReceivedData(srInfo);
//                } else {
//                    handler.sendEmptyMessage(INITDATAHOTSUCCESS_NODATA);
//                }
//                break;
//            default:
//                if (this.mRequsetHotTime > 0) {
//                    handler.sendEmptyMessage(SHOW_404);
//                } else {
//                    handler.sendEmptyMessage(READCACHE);
//                }
//                break;
//        }
//    }
//
//    /** 处理联网获取的数据 */
//    private void formatReceivedData(SearchRecommendInfo srInfo) {
//        if (this.mRequsetHotTime > 0) {
//            List<BookInfo> tempLabelInfoData = srInfo.getLabelList();
//            if (tempLabelInfoData != null && tempLabelInfoData.size() > 0) {
//                this.mSearchLabelInfoData.clear();
//                this.mSearchLabelInfoData = tempLabelInfoData;
//                handler.sendEmptyMessage(INITDATAHOTTABLESUCCESS);
//                // 保存数据到本地
//                LocalBookInfoManager.getInstance().saveLabelfo2Local(
//                        this.mSearchLabelInfoData,
//                        LocalBookInfoManager.getInstance()
//                                .getmBookSearchRecommendSearchLabelDataLocalDataName());
//                this.mIsRequsetHot = true;
//                this.mRequsetHotTime++;
//            }
//        } else {
//            List<BookInfo> tempGalleryInfoData = srInfo.getBooksList();
//            if (tempGalleryInfoData.size() > 0) {
//                this.mSearchGalleryInfoData.clear();
//                this.mSearchGalleryInfoData = tempGalleryInfoData;
//                // 保存数据到本地
//                LocalBookInfoManager.getInstance().saveBookInfo2Local(
//                        this.mSearchGalleryInfoData,
//                        LocalBookInfoManager.getInstance()
//                                .getmBookSearchRecommendGalleryDataLocalDataName());
//            }
//            this.mSearchLabelInfoData = srInfo.getLabelList();
//            if (this.mSearchLabelInfoData != null && this.mSearchLabelInfoData.size() > 0) {
//                // 保存数据到本地
//                LocalBookInfoManager.getInstance().saveLabelfo2Local(
//                        this.mSearchLabelInfoData,
//                        LocalBookInfoManager.getInstance()
//                                .getmBookSearchRecommendSearchLabelDataLocalDataName());
//            }
//            this.mIsRequsetHot = true;
//            handler.sendEmptyMessage(INITDATAHOTSUCCESS);
//            this.mRequsetHotTime++;
//        }
//    }
//
//    @Override
//    public void fragmentLogicForward() {
//        this.init();
//        this.initSetOnClickListener();
//        this.mLoadingLayout.setVisibility(View.VISIBLE);
//        this.mBookSearchRecoomendController.handle(0, "hot", "");
//    }
//
//    @Override
//    public void fragmentInitData() {
//        if (this.mBookSearchImageAdapter == null) {
//            this.mBookSearchImageAdapter =
//                    new BookSearchImageAdapter(mContext, this.mSearchGalleryInfoData);
//            if (mSearchGalleryInfoData != null && mSearchGalleryInfoData.size() > 0) {
//                this.mBookSearchImageAdapter.setGallerySecectId(this.mSearchGalleryInfoData.get(0)
//                        .getBookId());
//            }
//            this.mBookSearchGallery.setAdapter(this.mBookSearchImageAdapter);
//
//            this.mSearchLabellist.addHeaderView(this.mBookSearchGalleryHeadView); // 把headView添加到listview中
//
//            this.mBookSearchGallery.setSelection((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2)
//                    % this.mSearchGalleryInfoData.size());// TODO默认选中第一项
//        } else {
//            this.mBookSearchImageAdapter.setImageAdapter(this.mSearchGalleryInfoData);
//            if (mSearchGalleryInfoData != null && mSearchGalleryInfoData.size() > 0) {
//                this.mBookSearchImageAdapter.setGallerySecectId(this.mSearchGalleryInfoData.get(0)
//                        .getBookId());
//            }
//            this.mBookSearchGallery.setSelection((Integer.MAX_VALUE / 10)
//                    - (Integer.MAX_VALUE / 10) % this.mSearchGalleryInfoData.size());// TODO默认选中第一项
//            this.mBookSearchImageAdapter.notifyDataSetChanged();
//        }
//
//        if (this.mBookSearchLabellistRealAdapter == null) {
//            this.mBookSearchLabellistRealAdapter =
//                    new BookSearchLabellistRealAdapter(mContext, this.mSearchLabelInfoData);
//        }
//        if (this.mBookSearchLabellistAdapter == null) {
//            this.mBookSearchLabellistAdapter =
//                    new BookSearchLabellistAdapter(mContext, this.mBookSearchLabellistRealAdapter);
//            this.mBookSearchLabellistAdapter.setOnItemClickListener(this); // 设置标签监听
//            this.mSearchLabellist.setAdapter(this.mBookSearchLabellistAdapter);
//            this.mBookSearchLabellistAdapter.setNumColumns(4); // 设置列表每行显示3列
//        }
//        this.mLoadingLayout.setVisibility(View.GONE);
//        this.mActNoNetwork.setVisibility(View.GONE);
//    }
//
//    /** 联网成功无数据时调用 */
//    private void mShowNoData() {
//        this.mLoadingLayout.setVisibility(View.GONE);
//        this.mActNoNetwork.setVisibility(View.GONE);
//        this.mActNoDataView.setVisibility(View.VISIBLE);
//    }
//
//    /** 初始化 */
//    private void init() {
//        this.mBookSearchGalleryHeadView = new BookSearchGalleryHead(mContext);
//        this.mChangeLabelFooterView = new BookSearchFooterView(mContext);
//        this.mBookSearchGallery =
//                (BookSearchGallery) this.mBookSearchGalleryHeadView
//                        .findViewById(R.id.search_gallery_list);
//        mBookSearchGallery.setAnimation(null);
//        mBookSearchGallery.setAnimationDuration(0);
//
//        this.mSearchLabellist = (ListView) mCurretntLayout.findViewById(R.id.search_labellist);
//        this.mActNoNetwork = (View) mCurretntLayout.findViewById(R.id.include_error);
//        this.mLoadingLayout = (LinearLayout) mCurretntLayout.findViewById(R.id.include_loading);
//        mActNoDataView = (LinearLayout) mCurretntLayout.findViewById(R.id.nodata_layout);
//
//        this.mSearchTableBatchButton =
//                (TextView) this.mChangeLabelFooterView.findViewById(R.id.search_table_batchbt);
//
//        this.mSearchLabellist.addFooterView(this.mChangeLabelFooterView); // 把footerView添加到listview中
//
//    }
//
//    /** 设置控件监听事件 */
//    private void initSetOnClickListener() {
//        this.mBookSearchGallery.setOnItemSelectedListener(new MyOnItemSelectedListener());// 设置gallery滑动监听
//        this.mBookSearchGallery.setOnItemClickListener(new GalleryOnItemClickListener());
//        this.mBookSearchGallery.setmPager(this.mViewPager);
//        this.mSearchTableBatchButton.setOnClickListener(this);
//        this.mActNoNetwork.findViewById(R.id.retry).setOnClickListener(this);
//    }
//
//    /** 读取本地推荐书籍数据 */
//    private void readLocalHotSearchImgData() {
//        this.mSearchGalleryInfoData =
//                LocalBookInfoManager.getInstance().readLocal2BookInfo(
//                        LocalBookInfoManager.getInstance()
//                                .getmBookSearchRecommendGalleryDataLocalDataName());
//
//        if (this.mSearchGalleryInfoData == null || this.mSearchGalleryInfoData.size() == 0) {
//            this.mSearchGalleryInfoData = new ArrayList<BookInfo>();
//            handler.sendEmptyMessage(SHOW_404);
//            return;
//        } else {
//            if (this.mBookSearchImageAdapter == null) {
//                this.mBookSearchImageAdapter =
//                        new BookSearchImageAdapter(mContext, this.mSearchGalleryInfoData);
//                this.mBookSearchGallery.setAdapter(this.mBookSearchImageAdapter);
//                this.mSearchLabellist.addHeaderView(this.mBookSearchGalleryHeadView); // 把headView添加到listview中
//                this.mBookSearchImageAdapter.setGallerySecectId(this.mSearchGalleryInfoData.get(0)
//                        .getBookId());
//                this.mBookSearchGallery.setSelection((Integer.MAX_VALUE / 2)
//                        - (Integer.MAX_VALUE / 2) % this.mSearchGalleryInfoData.size());// TODO默认选中第一项
//            }
//            this.mIsRequsetHot = true;
//            this.mRequsetHotTime++;
//        }
//    }
//
//    /** 读取本地热搜推荐标签数据 */
//    private void readLocalHotSearchTableData() {
//        this.mSearchLabelInfoData =
//                LocalBookInfoManager.getInstance().readLocal2Labelfo(
//                        LocalBookInfoManager.getInstance()
//                                .getmBookSearchRecommendSearchLabelDataLocalDataName());
//
//        if (this.mSearchLabelInfoData == null || this.mSearchLabelInfoData.size() == 0) {
//            this.mSearchLabelInfoData = new ArrayList<BookInfo>();
//        } else {
//            if (this.mBookSearchLabellistRealAdapter == null) {
//                this.mBookSearchLabellistRealAdapter =
//                        new BookSearchLabellistRealAdapter(mContext, this.mSearchLabelInfoData);
//            }
//            if (this.mBookSearchLabellistAdapter == null) {
//                this.mBookSearchLabellistAdapter =
//                        new BookSearchLabellistAdapter(mContext,
//                                this.mBookSearchLabellistRealAdapter);
//                this.mBookSearchLabellistAdapter.setOnItemClickListener(this); // 设置标签监听
//                this.mSearchLabellist.setAdapter(this.mBookSearchLabellistAdapter);
//                this.mBookSearchLabellistAdapter.setNumColumns(4); // 设置列表每行显示3列
//            }
//        }
//    }
//
//    /** 标签点击事件 */
//    @Override
//    public void onItemClick(int pos, int realPos) {
//        if (this.mBookSearchLabellistRealAdapter != null) {
//            BookInfo info = this.mBookSearchLabellistRealAdapter.getItem(realPos);
//            Intent intent = new Intent(mContext, BookSearchResultActivity.class);
//            intent.putExtra("keyword", info.getBookName());
//            ActivityManager.getInstance().startNextActivity(intent, mContext.getParent());
//            SearchHistoryDao.getInstance().saveSearchHistory(info.getBookName()); // 保存到数据库
//        }
//    }
//
//    /** gallery事件监听 */
//    private class MyOnItemSelectedListener implements OnItemSelectedListener {
//        @Override
//        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//            clickGalleryShoeDesc(position);
//        }
//
//        @Override
//        public void onNothingSelected(AdapterView<?> parent) {}
//    }
//
//    /** 选中后book对象 */
//    private BookInfo bookInfo = null;
//
//    /** gallery点击事件 */
//    private class GalleryOnItemClickListener implements OnItemClickListener {
//
//        @Override
//        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            position = position % mSearchGalleryInfoData.size();
//            if (bookInfo == mSearchGalleryInfoData.get(position)) {
//                Intent intent = new Intent(mContext, BookSearchResultActivity.class);
//                intent.putExtra("keyword", bookInfo.getBookName());
//                ActivityManager.getInstance().startNextActivity(intent, mContext.getParent());
//                SearchHistoryDao.getInstance().saveSearchHistory(bookInfo.getBookName()); // 保存到数据库
//            } else {
//                return;
//            }
//
//        }
//    }
//
//    /** 滑动或点击gallery切换不同的显示内容 */
//    private void clickGalleryShoeDesc(int position) {
//        if (this.mSearchGalleryInfoData.size() > 0) {
//            position = position % this.mSearchGalleryInfoData.size();
//            bookInfo = this.mSearchGalleryInfoData.get(position);
//            TextView textName =
//                    (TextView) this.mBookSearchGalleryHeadView
//                            .findViewById(R.id.search_gallery_bookname);
//            textName.setText(bookInfo.getBookName());
//            TextView textAuthor =
//                    (TextView) this.mBookSearchGalleryHeadView
//                            .findViewById(R.id.search_gallery_bookauthor);
//            textAuthor.setText(bookInfo.getBookAuthorName());
//            TextView textDesc =
//                    (TextView) this.mBookSearchGalleryHeadView
//                            .findViewById(R.id.search_gallery_bookdesc);
//            textDesc.setText(bookInfo.getBookIntro());
//
//            this.mBookSearchImageAdapter.setGallerySecectId(bookInfo.getBookId());
//            this.mBookSearchImageAdapter.notifyDataSetChanged();
//            // 设置文字区域点击事件
//            LinearLayout search_gallery_alltextlayout =
//                    (LinearLayout) this.mBookSearchGalleryHeadView
//                            .findViewById(R.id.search_gallery_alltextlayout);
//            search_gallery_alltextlayout.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(mContext, BookSearchResultActivity.class);
//                    intent.putExtra("keyword", bookInfo.getBookName());
//                    ActivityManager.getInstance().startNextActivity(intent, mContext.getParent());
//                    SearchHistoryDao.getInstance().saveSearchHistory(bookInfo.getBookName()); // 保存到数据库
//                }
//            });
//        }
//    }
//
//    @Override
//    public void request_content_io() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_reponse_io() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_parse_io() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_parse_format() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_net_error() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_timeout() {
//        // TODO Auto-generated method stub
//
//    }
//
//    @Override
//    public void request_url() {
//        // TODO Auto-generated method stub
//
//    }
//
//}
